/*
 * @Author: LGX
 * @Date: 2022/5/1
 * @Description:
 */

import { Service } from 'egg';

export default class DiaryService extends Service {
  // 新增
  async addDiary({ title, context, startime }) {
    const { ctx } = this;
    return ctx.model.Diary.create({
      title,
      context,
      startime,
    });
  }
  // 修改
  async updateDiary(params, id) {
    return this.ctx.model.Diary.update(params, { where: { id } });
  }
  // 删除
  async deleteDiary(id) {
    return this.ctx.model.Diary.destroy({ where: { id } });
  }
  // 分页查询
  async getDiary({ page, pageSize }: any) {
    // const where = { status: 1 }
    const { count, rows } = await this.ctx.model.Diary.findAndCountAll({
      // where,
      offset: (page - 1) * pageSize,
      limit: pageSize,
      order: [[ 'createdAt', 'DESC' ]],
    });
    return {
      record: rows,
      currentPage: Number(page),
      pageSize,
      pages: Math.ceil(count / pageSize),
      total: count,
    };
  }
  // 查找对应数据
  async findDiary({ ...params }) {
    const { ctx } = this;
    return ctx.model.Diary.findOne({
      where: params,
    });
  }
}
